//
//  UIView+PJAdaptScreen.m
//  SSMS
//
//  Created by yelihua on 2018/12/17.
//  Copyright © 2018 yelihua. All rights reserved.
//

#import "UIView+PJAdaptScreen.h"
#import "TYUIKit/TYUIKit.h"
#import "YYCategories/YYCategories.h"


@implementation UIView (PJAdaptScreen)

- (BOOL)hasAdapt{
    return [self getAssociatedValueForKey:_cmd];
}

- (void)setHasAdapt:(BOOL)hasAdapt{
    [self setAssociateWeakValue:@(hasAdapt) withKey:@selector(hasAdapt)];
}

- (void)adaptScreen{
    [self adaptScreenWidthWithType:PJAdaptScreenWidthTypeAll];
}

- (void)adaptScreenWidthWithType:(PJAdaptScreenWidthType)type {
    [self adaptScreenWidthWithType:type refereWidth:375 exceptViews:nil];
}

- (void)adaptScreenWidthWithType:(PJAdaptScreenWidthType)type
                     exceptViews:(nullable NSArray<Class> *)exceptViews {
    [self adaptScreenWidthWithType:type refereWidth:375 exceptViews:exceptViews];
}

- (void)adaptScreenWidthWithType:(PJAdaptScreenWidthType)type refereWidth:(CGFloat)refereWidth exceptViews:(nullable NSArray<Class> *)exceptViews
{
    CGFloat x = TY_ScreenMultiplyCoefficient() * MIN(SCREENWIDTH, SCREENHEIGHT) / refereWidth ;
    [self adaptScreenWidthWithType:type rate:x exceptViews:exceptViews];
}

- (void)adaptScreenWidthWithType:(PJAdaptScreenWidthType)type rate:(CGFloat)rate exceptViews:(nullable NSArray<Class> *)exceptViews
{
    if (type == PJAdaptScreenWidthTypeNone)  return;
    if (self.hasAdapt) {
        return;
    }
    if (![self isExceptViewClassWithClassArray:exceptViews]) {
        self.hasAdapt = YES;
        CGFloat x = rate;
        
        // 是否要对约束进行等比例
        BOOL adaptConstraint = ((type & PJAdaptScreenWidthTypeConstraint) || type == PJAdaptScreenWidthTypeAll);
        
        // 是否对字体大小进行等比例
        BOOL adaptFontSize = ((type & PJAdaptScreenWidthTypeFontSize) || type == PJAdaptScreenWidthTypeAll);
        
        // 是否对圆角大小进行等比例
        BOOL adaptCornerRadius = ((type & PJAdaptScreenWidthTypeCornerRadius) || type == PJAdaptScreenWidthTypeAll);
        
        // 约束
        if (adaptConstraint) {
            [self.constraints enumerateObjectsUsingBlock:^(__kindof NSLayoutConstraint * _Nonnull subConstraint, NSUInteger idx, BOOL * _Nonnull stop) {
                subConstraint.constant = subConstraint.constant * x;
            }];
        }
        
        // 字体大小
        if (adaptFontSize) {
            
            UIFont* (^fontSet)(UIFont*) = ^(UIFont* font){
                return [UIFont fontWithDescriptor:font.fontDescriptor size:font.pointSize * x];
            };
            
            
            if ([self isKindOfClass:[UILabel class]] && ![self isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
                UILabel *labelSelf = (UILabel *)self;
                labelSelf.font = fontSet(labelSelf.font);
            }
            else if ([self isKindOfClass:[UITextField class]]) {
                UITextField *textFieldSelf = (UITextField *)self;
                textFieldSelf.font = fontSet(textFieldSelf.font);
            }
            else  if ([self isKindOfClass:[UIButton class]]) {
                UIButton *buttonSelf = (UIButton *)self;
                buttonSelf.titleLabel.font = fontSet(buttonSelf.titleLabel.font);
            }
            else  if ([self isKindOfClass:[UITextView class]]) {
                UITextView *textViewSelf = (UITextView *)self;
                textViewSelf.font = fontSet(textViewSelf.font);
            }
        }
        
        // 圆角
        if (adaptCornerRadius) {
            if (self.layer.cornerRadius) {
                self.layer.cornerRadius = self.layer.cornerRadius * x;
            }
        }
        
        [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull subView, NSUInteger idx, BOOL * _Nonnull stop) {
            // 继续对子view操作
            [subView adaptScreenWidthWithType:type rate:rate exceptViews:exceptViews];
        }];
    }
}

// 当前view对象是否是例外的视图
- (BOOL)isExceptViewClassWithClassArray:(NSArray<Class> *)classArray {
    __block BOOL isExcept = NO;
    [classArray enumerateObjectsUsingBlock:^(Class  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([self isKindOfClass:obj]) {
            isExcept = YES;
            *stop = YES;
        }
    }];
    return isExcept;
}

@end
